package com.itheima.service.db.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.db.sql.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.*;
import com.itheima.domain.vo.*;
import com.itheima.mapper.*;
import com.itheima.service.db.LingHunService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@DubboService
public class LingHunServiceImpl implements LingHunService {
  
    @Autowired
    private QuestionnaireMapper questionnaireMapper;
    @Autowired
    private SoulQuestionMapper soulQuestionMapper;
    @Autowired
    private SoulQuestionOptionMapper soulQuestionOptionMapper;
    @Autowired
    private QuestionnaireResultMapper questionnaireResultMapper;
    @Autowired
    private QuestionnaireReportMapper questionnaireReportMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserMapper userMapper;
    //问卷列表
    @Override
    public List<QuestionnaireVo> testSoul(Long userId) {
        List<QuestionnaireVo> voList=new ArrayList<>();
        QueryWrapper<Questionnaire> queryWrapper=new QueryWrapper<>();
        List<Questionnaire> questionnaireList = questionnaireMapper.selectList(queryWrapper);
        for (Questionnaire questionnaire : questionnaireList) {
            QuestionnaireVo questionnaireVo=new QuestionnaireVo();
            QueryWrapper<SoulQuestion> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("questionnaire_id",questionnaire.getId());
            List<SoulQuestion> soulQuestionList = soulQuestionMapper.selectList(queryWrapper1);
            List<SoulQuestionVo> soulQuestionVoList =new ArrayList<>();
            for (SoulQuestion soulQuestion : soulQuestionList) {
                SoulQuestionVo soulQuestionVo=new SoulQuestionVo();
                QueryWrapper<SoulQuestionOption> queryWrapper2=new QueryWrapper<>();
                queryWrapper2.eq("question_id",soulQuestion.getId());
                List<SoulQuestionOption> soulQuestionOptionList = soulQuestionOptionMapper.selectList(queryWrapper2);
                List<SoulQuestionOptionVo> soulQuestionOptionVoList=new ArrayList<>();
                for (SoulQuestionOption soulQuestionOption : soulQuestionOptionList) {
                    SoulQuestionOptionVo soulQuestionOptionVo=new SoulQuestionOptionVo();
                    soulQuestionOptionVo.setOption(soulQuestionOption.getContent());
                    soulQuestionOptionVo.setId(String.valueOf(soulQuestionOption.getId()));
                    soulQuestionOptionVoList.add(soulQuestionOptionVo);
                }
                soulQuestionVo.setOptions(soulQuestionOptionVoList);
                soulQuestionVo.setId(String.valueOf(soulQuestion.getId()));
                soulQuestionVo.setQuestion(soulQuestion.getStem());
                soulQuestionVoList.add(soulQuestionVo);
            }
            questionnaireVo.setCover(questionnaire.getCover());
            questionnaireVo.setStar(questionnaire.getStar());
            questionnaireVo.setName(questionnaire.getName());
            questionnaireVo.setId(String.valueOf(questionnaire.getId()));
            questionnaireVo.setLevel(questionnaire.getLevel());
            questionnaireVo.setIsLock(0);
            questionnaireVo.setQuestions(soulQuestionVoList);
            voList.add(questionnaireVo);
        }
        return voList;
    }
    //题目提交
    public void testSoul(Long userId, List<AnswersVo> answers) {
        Integer score = 0;
        Long questionId = 0L;
        //计算分数
        for (AnswersVo answersVo : answers) {
            questionId =Long.valueOf(answersVo.getQuestionId());
            Long optionId = Long.valueOf(answersVo.getOptionId());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("id", optionId);
            SoulQuestionOption soulQuestionOption = soulQuestionOptionMapper.selectOne(queryWrapper);
            score += Integer.parseInt(soulQuestionOption.getScore());
        }
        System.out.println("总分:"+score);
        QueryWrapper<SoulQuestion> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",questionId);
        SoulQuestion soulQuestion = soulQuestionMapper.selectOne(queryWrapper);
        Long questionnaireId = soulQuestion.getQuestionnaireId();
        QuestionnaireReport questionnaireReport=new QuestionnaireReport();
        QueryWrapper<QuestionnaireResult> queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("questionnaire_id",questionnaireId);
        List<QuestionnaireResult> questionnaireResultList = questionnaireResultMapper.selectList(queryWrapper1);
        for (QuestionnaireResult questionnaireResult : questionnaireResultList) {
            String[] strings = questionnaireResult.getScope().split(",");
            Integer[] integers=new Integer[2];
            for (int i = 0; i < strings.length; i++) {
                integers[i]=Integer.valueOf(strings[i]);
            }
            if (score<=integers[1]&&score>=integers[0]){
                questionnaireReport.setConclusionId(String.valueOf(questionnaireResult.getId()));
                questionnaireReport.setCover(questionnaireResult.getCover());
            }
        }
        questionnaireReport.setExtroversion(String.valueOf(score));
        questionnaireReport.setUserId(String.valueOf(userId));
        QueryWrapper<QuestionnaireReport> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("user_id",userId);
        QuestionnaireReport report = questionnaireReportMapper.selectOne(queryWrapper2);
        if (report==null){
            questionnaireReportMapper.insert(questionnaireReport);
        }else {
            questionnaireReportMapper.update(questionnaireReport,queryWrapper2);
        }
    }
    //查看结果
    public TestResultVo report(Long userId) {
        TestResultVo testResultVo = new TestResultVo();
        QueryWrapper<QuestionnaireReport> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("user_id",userId);
        System.out.println("userId"+userId);
        QuestionnaireReport questionnaireReport = questionnaireReportMapper.selectOne(queryWrapper1);
        QueryWrapper<QuestionnaireResult> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("id",questionnaireReport.getConclusionId());
        QuestionnaireResult questionnaireResult = questionnaireResultMapper.selectOne(queryWrapper2);
        testResultVo.setConclusion(questionnaireResult.getContent());
        testResultVo.setCover(questionnaireResult.getCover());
        List<Dimensions> dimensionsList=new ArrayList<>();
            Dimensions dimensions=new Dimensions("总分",questionnaireReport.getExtroversion());
            dimensionsList.add(dimensions);
        testResultVo.setDimensions(dimensionsList);
        QueryWrapper<UserInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",1);
        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
        List<SimilarYou> similarYouList=new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            Long userInfoId = userInfo.getId();
            Integer integer = userInfoId.intValue();
            similarYouList.add(new SimilarYou(integer,userInfo.getAvatar()));
        }
        testResultVo.setSimilarYou(similarYouList);
        return testResultVo;
    }
}
